package com.hsw.habit.frame.utils;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.Bucket;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;

import java.io.File;
import java.io.InputStream;
import java.util.List;

public class OssUtil {
    private static OssUtil ossUtil;

    private COSClient cosClient;

    public OssUtil() {
        String secretId = "AKIDtjndGaHUFpxribpgwxi4B8pGWWHOsatx";
        String secretKey = "93j9LP0NVO2ScEg2B5mDTUQ8a6JODm6B";
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        Region region = new Region("ap-shanghai");
        ClientConfig clientConfig = new ClientConfig(region);
        cosClient = new COSClient(cred, clientConfig);
    }

    public static OssUtil getInstance() {
        if (null == ossUtil) {
            ossUtil = new OssUtil();
        }
        return ossUtil;
    }

    public List<Bucket> getBucketList() {
        try {
            return cosClient.listBuckets();
        } catch (CosServiceException serverException) {
            serverException.printStackTrace();
        } catch (CosClientException clientException) {
            clientException.printStackTrace();
        }
        return null;
    }

    public String uploadFile(File file) {
        try {
            String bucketName = "hsw-1251827437";
            // 指定要上传到 COS 上对象键
            String key = "test";
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            if (null != putObjectResult) {
                return putObjectResult.getDateStr();
            }
        } catch (CosServiceException serverException) {
            serverException.printStackTrace();
        } catch (CosClientException clientException) {
            clientException.printStackTrace();
        }
        return "";
    }



    public String uploadFileByInputStream(InputStream inputStream) {
        try {
            String bucketName = "hsw-1251827437";
            // 指定要上传到 COS 上对象键
            String key = MD5Util.md5(String.valueOf(System.currentTimeMillis())) + ".jpg";
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType("image/jpeg");
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream,metadata);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);


            if (null != putObjectResult) {
                return "https://" + bucketName + ".cos.ap-shanghai.myqcloud.com/" + key;
            }
        } catch (CosServiceException serverException) {
            serverException.printStackTrace();
        } catch (CosClientException clientException) {
            clientException.printStackTrace();
        }
        return "";
    }
}
